﻿using System;
using System.Diagnostics;
using System.IO;

class Program
{
    static void Main()
    {
        var fullPath = Path.GetFullPath("hello.ps1");

        // 定义 PowerShell 脚本的路径
        string scriptPath = fullPath;

        // 创建 ProcessStartInfo 对象
        ProcessStartInfo startInfo = new ProcessStartInfo()
        {
            FileName = "powershell.exe",
            Arguments = $"-NoProfile -ExecutionPolicy Bypass -File \"{scriptPath}\" -Message HelloWorld",
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        // 启动 PowerShell 进程
        using (Process process = new Process())
        {
            process.StartInfo = startInfo;
            process.OutputDataReceived += (sender, e) => Console.WriteLine(e.Data);
            process.ErrorDataReceived += (sender, e) => Console.WriteLine("ERROR: " + e.Data);

            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();

            process.WaitForExit();
        }
    }
}